<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Gass_news_model extends CI_Model {
	
	// The following method prevents an error occurring when $this->data is modified.
	// Error Message: 'Indirect modification of overloaded property Demo_cart_admin_model::$data has no effect'.
	public function &__get($key)
	{
		$CI =& get_instance();
		return $CI->$key;
	}
	/**
	 * 编辑提交公告消息待审批
	 *
	 */
	 function add_public_news()
	 {
	 	 $this->load->library('form_validation');
		 $this->form_validation->set_rules('add_news_title', '公告标题', 'required|max_length[150]');
		 $this->form_validation->set_rules('news_edit_text', '正文', 'required');
     // Run the validation.
		 if ($this->form_validation->run()){
		 	 $data = array(
               'news_title' => $_POST['add_news_title'],
               'news_content' => $_POST['news_edit_text'],
               'news_create_person' => $this->flexi_auth->get_user_id(),
               'create_time' => date('Y-m-d H:i:s',time()),
               'news_status' => 1
            );
       $this->db->insert('ga_public_news', $data); 
		 	 redirect("gass_news/manage_public_news");

	   }		
	   else{	
			// Set validation errors.
			$this->data['message'] = validation_errors('<p class="error_msg">', '</p>');
			return FALSE;
		}
	}
	/**
	 * 获取公告信息列表
	 * 
	 * 研招办老师可以获取所有的信息
	 * 学院老师只能看到自己发的公告信息
	 */	
	function get_all_public_news(){
		
		$uri = $this->uri->uri_to_assoc(3);     
		$limit = $this->config->item('pag_limit');
		$offset = (isset($uri['page'])) ? $uri['page'] : FALSE;	
		if (array_key_exists('search_key_words', $uri)){
			$pagination_url = 'gass_news/manage_public_news/search_key_words/'.$uri['search_key_words'].'/select_news_status/'.$uri['select_news_status'].'/';
			$config['uri_segment'] = 8; // Changing to 6 will select the 6th segment, example 'controller/function/search/query/page/10'.
	    
	    $search_key_words = str_replace('-',' ',urldecode($uri['search_key_words']));
	    $select_news_status = str_replace('-',' ',urldecode($uri['select_news_status']));
			
      $this->db->select('*');
      $this->db->from('ga_public_news');
      $where = '';			

      if ($select_news_status!='all'){
      	$where = "news_status = ".$select_news_status;
      }else{
      	$where = "(news_status = 1 or news_status = 2)";
      }
      
      if ($search_key_words!='0000'){
      	$where = $where." and ( news_title like '%".$search_key_words."%' or news_content like '%".$search_key_words."%' )";
      }
      if($this->flexi_auth->in_group('college teacher'))
        $where = $where." and news_create_person = ".$this->flexi_auth->get_user_id(); 
      
      $this->db->where($where);
      $this->db->order_by("create_time", "desc");
      $this->db->limit($limit, $offset);
      $query =$this->db->get();
      $this->db->select('*');
      $this->db->from('ga_public_news');    
      $this->db->where($where);
			$querynum = $this->db->get();
 			$total_public_news = $querynum->num_rows();				
			$this->data['newsListInfo'] = $query->result_array();	      
            
	  }else{
	    $search_key_words	='';
	  	$select_news_status	='';	  	
			// Set some defaults.
			$pagination_url = 'gass_news/manage_public_news/';
			$search_query = FALSE;
			$config['uri_segment'] = 4; // Changing to 4 will select the 4th segment, example 'controller/function/page/10'.
		
      $this->db->select('*');
      $this->db->from('ga_public_news');
      if($this->flexi_auth->in_group('college teacher'))
        $this->db->where('news_create_person',$this->flexi_auth->get_user_id());
      $this->db->order_by("create_time", "desc"); 
      $this->db->limit($limit, $offset);
      $query =$this->db->get();
      if($this->flexi_auth->in_group('college teacher'))
        $querynum = $this->db->get_where('ga_public_news',array('news_create_person'=>$this->flexi_auth->get_user_id()));
      else
			  $querynum = $this->db->get('ga_public_news');
 			$total_public_news = $querynum->num_rows();				
			
			$this->data['newsListInfo'] = $query->result_array();	
			
	  }
	  
		// Create user record pagination.
		$this->load->library('pagination');	
		$config['base_url'] = base_url().$pagination_url.'page/';
		$config['total_rows'] = $total_public_news;
		$config['per_page'] = $limit; 
		$this->pagination->initialize($config); 
		
		$this->data['search_key_words'] = ($search_key_words!='0000')?$search_key_words:'';
		$this->data['select_news_status'] = $select_news_status; 
		$this->data['pagination']['links'] = $this->pagination->create_links();
		$this->data['pagination']['total_public_news'] = $total_public_news;
	}	
	/**
	 * get_all_auth_public_news
	 * 获取所有已审批过的公告
	 */	
	function get_all_auth_public_news()
	{
		// Get url for any search query or pagination position.
		$uri = $this->uri->uri_to_assoc(3);     
		// Set pagination limit, get current position and get total users.
		$limit = $this->config->item('pag_limit');
		$offset = (isset($uri['page'])) ? $uri['page'] : FALSE;	
		
		if (array_key_exists('search', $uri)){
			// Set pagination url to include search query.
			$pagination_url = 'gass_news/manage_public_news/search/'.$uri['search'].'/';
			$config['uri_segment'] = 6; // Changing to 6 will select the 6th segment, example 'controller/function/search/query/page/10'.
	    $search_query = str_replace('-',' ',urldecode($uri['search']));
			/*
			$this->db->select('*');
			$this->db->from('info_supplier');
      $where = "info_supplier.upro_id = ".$this->flexi_auth->get_user_id()." and ( suppl_brand like '%".$search_query."%' or suppl_location like '%".$search_query."%')";
      $this->db->where($where);
      $this->db->join('jadiiar_user_profiles', 'info_supplier.upro_id = jadiiar_user_profiles.upro_id');
      $this->db->limit($limit, $offset);
      $query = $this->db->get();
     
			$this->db->select('*');
			$this->db->from('info_supplier');
      //$where = "info_supplier.upro_id = ".$this->flexi_auth->get_user_id()." and ( suppl_brand like '%".$search_query."%' or suppl_location like '%".$search_query."%')";
      $this->db->where($where);
      $this->db->join('jadiiar_user_profiles', 'info_supplier.upro_id = jadiiar_user_profiles.upro_id');
      $querynum1 = $this->db->get()->num_rows();        
      $total_suppliers = $querynum1;//$querynum1->num_rows();			  
		    
		  
 			$this->data['newsListInfo'] = $query->result_array();	 
	    */
	  }else{	  	
			// Set some defaults.
			$pagination_url = 'gass_news/manage_public_news/';
			$search_query = FALSE;
			$config['uri_segment'] = 4; // Changing to 4 will select the 4th segment, example 'controller/function/page/10'.
		
      $this->db->select('*');
      $this->db->from('ga_public_news');
      $this->db->where('news_status',2);
      $this->db->limit($limit, $offset);
      $query =$this->db->get();
			$querynum = $this->db->get_where('ga_public_news',array('news_status' => 2));
 			$total_public_news = $querynum->num_rows();				
			
			$this->data['newsListInfo'] = $query->result_array();	
			
	  }
	  
		// Create user record pagination.
		$this->load->library('pagination');	
		$config['base_url'] = base_url().$pagination_url.'page/';
		$config['total_rows'] = $total_public_news;
		$config['per_page'] = $limit; 
		$this->pagination->initialize($config); 
		
		// Make search query and pagination data available to view.
		$this->data['search_query'] = $search_query; // Populates search input field in view.
		$this->data['pagination']['links'] = $this->pagination->create_links();
		$this->data['pagination']['total_public_news'] = $total_public_news;
	}	
	
	/**
	 * get_public_news_details
	 * 获取公告细节
	 */	
	function get_public_news_details($pnId){
		return $this->db->get_where('ga_public_news',array('news_id' => $pnId))->row_array();	
	}
	/**
	 * public_news_authorization
	 * 审批公告
	 */	
	function public_news_authorization($pnId){
    $data = array(
               'news_status' => 2,
               'news_approval_person' => $this->flexi_auth->get_user_id(),
               'approval_time' => date('Y-m-d H:i:s',time())
            );
    $this->db->where('news_id', $pnId);
    $this->db->update('ga_public_news', $data); 	
    redirect("gass_news/manage_public_news");
	}	
	
 	/**
	 * 删除公告
	 * 
	 * 研招办老师可以删除已审批或未审批的公告
	 * 二级招生办老师只能删除未审核的公告
	 */
	function update_public_news()
	{
			if ($delete_news = $this->input->post('delete_news')){
				foreach($delete_news as $news_id => $delete){
					if($this->flexi_auth->in_group('admin teacher'))
					  $this->db->delete('ga_public_news', array('news_id' => $news_id)); 
					else
					  $this->db->delete('ga_public_news', array('news_id' => $news_id,'news_status' => 1)); 
				}
			}
		$this->session->set_flashdata('message', $this->flexi_auth->get_messages());
		redirect('gass_news/manage_public_news');			
	}	
}
